package com.bjpowernode.p2p.comtroller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.p2p.constant.Loan;
import com.bjpowernode.p2p.constant.Page;
import com.bjpowernode.p2p.constant.Report;
import com.bjpowernode.p2p.constant.WebPath;
import com.bjpowernode.p2p.entity.LoanInfo;
import com.bjpowernode.p2p.service.loan.LoanService;
import com.bjpowernode.p2p.service.user.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;

@Controller
public class IndexController {

    //check代表是否检查服务，默认为true,即服务启动，消费者才能启动
    //实际情况的设置为check=false,不能因为服务没有启动，消费者启动不了
    //服务消费者，version = "1.0",check =false两个参数必须跟
    @Reference(interfaceClass = LoanService.class, version = "1.0", check = false)
    LoanService loanService;

    @Reference(interfaceClass = UserService.class, version = "1.0", check = false)
    UserService userService;


    @RequestMapping(WebPath.INDEX_PATH)
    public String index(Model model) {

        //动力金融网历史年化收益率
        double avgLoanRate = loanService.avgLoanRate();
        model.addAttribute(Report.AVG_LOAN_RATE, avgLoanRate);
        //平台用户数
        Integer countUser = userService.countUser();
        model.addAttribute(Report.COUNT_USER, countUser);
        //累计成交额
        double sumBidMoney = loanService.sumBidMoney();
        model.addAttribute(Report.SUM_BID_MONEY, sumBidMoney);
        //新手产品宝首页展示
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put(Loan.PRODUCT_TYPE, 0);
        hashMap.put(Page.CURRENT_PAGE, 0);
        hashMap.put(Page.PAGE_SIZE, 1);
        List<LoanInfo> XSlist = loanService.queryByproductType(hashMap);
        model.addAttribute(Loan.XS_LIST, XSlist);
        //优选产品首页展示
        hashMap.put(Loan.PRODUCT_TYPE, 1);
        hashMap.put(Page.CURRENT_PAGE, 0);
        hashMap.put(Page.PAGE_SIZE, 4);
        List<LoanInfo> YXlist = loanService.queryByproductType(hashMap);
        model.addAttribute(Loan.YX_LIST, YXlist);
        //散标产品页展示
        hashMap.put(Loan.PRODUCT_TYPE, 2);
        hashMap.put(Page.CURRENT_PAGE, 0);
        hashMap.put(Page.PAGE_SIZE, 8);
        List<LoanInfo> SBlist = loanService.queryByproductType(hashMap);
        model.addAttribute(Loan.SB_LIST, SBlist);
        return WebPath.INDEX_RETURN;
    }
}
